<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  
  <title>搞定INI配置文件 | Wentao&#39;s Blog</title>

  
  <meta name="author" content="wentao">
  

  
  <meta name="description" content="Ini的陷阱
测试的时候发现了一个问题.ini配置文件第一行如果未section的话.是无法读到的.解决办法也有就是空一行就万事大吉了.
今天下午刚好又遇到了.然后就想着把这个解决了.之前为什么不想动的原因是因为这个读取使用的Windows的接口.很有可能这东西就有这么个问题…
大家读取ini的方法">
  

  
  
  <meta name="keywords" content="ini,nini">
  

  <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  <meta property="og:title" content="搞定INI配置文件"/>

  <meta property="og:site_name" content="Wentao&#39;s Blog"/>

  
  <meta property="og:image" content="/favicon.ico"/>
  

  <link href="/favicon.ico" rel="icon">
  <link rel="alternate" href="/atom.xml" title="Wentao&#39;s Blog" type="application/atom+xml">
  <link rel="stylesheet" href="/css/style.css" media="screen" type="text/css">
</head>


<body>
<div class="blog">
  <div class="content">

    <header>
  <div class="site-branding">
    <h1 class="site-title">
      <a href="/">Wentao&#39;s Blog</a>
    </h1>
    <p class="site-description"></p>
  </div>
  <nav class="site-navigation">
    <ul>
      
        <li><a href="/">主页</a></li>
      
        <li><a href="/archives">归档</a></li>
      
    </ul>
  </nav>
</header>

    <main class="site-main posts-loop">
    <article>

  
    
    <h3 class="article-title"><span>搞定INI配置文件</span></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/2013/01/21/Work-with-ini/" rel="bookmark">
        <time class="entry-date published" datetime="2013-01-21T02:18:00.000Z">
          2013-01-21
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h1>Ini的陷阱</h1>
<p>测试的时候发现了一个问题.<code>ini配置文件</code>第一行如果未<code>section</code>的话.是无法读到的.解决办法也有就是空一行就万事大吉了.</p>
<p>今天下午刚好又遇到了.然后就想着把这个解决了.之前为什么不想动的原因是因为这个读取使用的Windows的接口.很有可能这东西就有这么个问题…</p>
<p>大家读取ini的方法其实基本上都是一样的,用windows的api.网上也有各种例子.大差不差吧.出问题就处在<a href="http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms724363.aspx" target="_blank" rel="external">GetProfileSection</a>
这个接口上了.第一行如果是小节[section]的话,是无法读取的…</p>
<h1>NIni</h1>
<p>想想读写ini,用C#单写其实也不差.后来发现有一个<a href="http://nini.sourceforge.net" target="_blank" rel="external">Nini</a>的框架.就很好的实现了配置文件的读写.比Windows API的方式方便多了.</p>
<p>Nini的特性
Features</p>
<ul>
<li>Multiple configuration types INI, XML, Registry, and command line</li>
<li>Strong variable types&lt;br/&gt; String, int, float, etc. Eliminates casts</li>
<li>Set and save&lt;br/&gt; Add, remove, edit, and save configs</li>
<li>Lightweight and fast&lt;br/&gt; Small footprint, built for speed</li>
<li>Merging&lt;br/&gt; Merge several configs into one</li>
<li>100% free&lt;br/&gt; Free and open source code</li>
<li>Value aliases&lt;br/&gt; Add aliases for unclear variables</li>
<li>Key value replacement&lt;br/&gt; Replaces values with other key values</li>
<li>Cross platform&lt;br/&gt; Run on .NET/Mono Linux/Mac/Windows</li>
<li>INI parser&lt;br/&gt; Contains a 100% managed INI parser</li>
<li>Fully documented&lt;br/&gt; See the Nini manual and API reference</li>
<li>Unlimited files/sources&lt;br/&gt; Loads an unlimited number of files</li>
<li>Compact Framework&lt;br/&gt; Supports the .NET Compact Framework</li>
<li>Command line application&lt;br/&gt; Has a command-line configuration editor</li>
<li>Mature and stable&lt;br/&gt; Over 140 unit tests</li>
</ul>
<p>以上这些特性简直完美了.特别是自带配置编辑器.合并配置文件,强类型,替换,文档全.这些都是很不错的.
.
接下来这一周希望把Nini的源码看一遍.以后工作中项目涉及到配置文件的,不出意外应该都会使用到这个神器</p>

      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    
      

    <span class="post-categories">
      <i class="icon-categories"></i>
        <a href="/categories/Dev/">Dev</a>
    </span>
    

    
    

    <span class="post-tags">
      <i class="icon-tags"></i>
        <a href="/tags/ini-nini/">ini,nini</a>
    </span>
    

    </div>

    
  </div>
</article>

  
	<div id="comment">
	
	
	<!-- 多说评论框 start -->
	 <div class="ds-thread" data-thread-key="/2013/01/21/Work-with-ini/" data-title="搞定INI配置文件" data-url="http://wentao.org/2013/01/21/Work-with-ini/"></div>
	<!-- 多说评论框 end -->
	<!-- 多说公共JS代码 start (一个网页只需插入一次) -->
	<script type="text/javascript">
	var duoshuoQuery = {short_name:"wentao"};
	  (function() {
	    var ds = document.createElement('script');
	    ds.type = 'text/javascript';ds.async = true;
	    ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
	    ds.charset = 'UTF-8';
	    (document.getElementsByTagName('head')[0] 
	     || document.getElementsByTagName('body')[0]).appendChild(ds);
	  })();
	  </script>
	<!-- 多说公共JS代码 end -->
	
	</div>




    </main>

    <footer class="site-footer">
  <p class="site-info">
    Proudly powered by <a href="https://hexo.io/" target="_blank">Hexo</a> and
    Theme by <a href="https://github.com/CodeDaraW/Hacker" target="_blank">Hacker</a>
    </br>
    
    &copy; 2016 wentao
    
  </p>
</footer>
    
  </div>
</div>
</body>
</html>